package com.xwaf.platform.system.api.util.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;

import java.util.List;

/**
 * 
 * <p>
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2020年1月8日 上午9:02:21 <br>
 * @see 无<br>
 *      Copyright (c) 2020, xwaf_love@yeah.net All Rights Reserved.<br>
 */
public class ObjectCodec extends MessageToMessageCodec<ByteBuf, Object> {

	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
		byte[] data = ObjectSerializerUtils.serilizer(msg);
		ByteBuf buf = Unpooled.buffer();
		buf.writeBytes(data);
		out.add(buf);
	}

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) {
		byte[] bytes = new byte[msg.readableBytes()];
		msg.readBytes(bytes);
		Object deSerilizer = ObjectSerializerUtils.deSerilizer(bytes);
		out.add(deSerilizer);
	}
}
